package com.itheima.controller;

import com.itheima.common.PageBean;
import com.itheima.common.PaginationVO;
import com.itheima.common.Result;
import com.itheima.entity.addRole;
import com.itheima.service.RoleService;
import com.itheima.vo.RoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色控制器
 *
 * @author 13225
 * @date 2023/09/21
 */
@RestController
@RequestMapping("/sys_role")
@Slf4j
public class RoleController {
    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 分页
     *
     * @return {@link Result}<{@link ?}>
     */
    @GetMapping("/list")
    public Result<PaginationVO<RoleVO>> page(Integer limit, Integer page, String name) {
        PaginationVO<RoleVO> pageBean = roleService.page(limit, page, name);
        return Result.success(pageBean);
    }

    /**
     * 所有数据
     *
     * @return {@link Result}<{@link ?}>
     */
    @GetMapping("/allData")
    public Result<?> allData() {
        List<RoleVO> roleList = roleService.allData();
        return Result.success(roleList);
    }

    /**
     * 添加角色
     *
     * @param role 角色
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/add")
    public Result<?> addRole(@RequestBody addRole role) {
        roleService.addRole(role);
        return Result.success();
    }

    /**
     * 行信息角色
     *
     * @param id id
     * @return {@link Result}<{@link ?}>
     */
    @GetMapping("/rowInfo")
    public Result<?> rowInfoRole(String id) {
        RoleVO roleVO = roleService.rowInfoRole(id);
        return Result.success(roleVO);
    }

    /**
     * 按iddel角色
     *
     * @param id id
     * @return {@link Result}<{@link ?}>
     */
    @GetMapping("/del")
    public Result<?> delRoleById(String id) {
        //记录日志
        log.info("根据id删除角色:{}", id);
        //调用业务层分页查询功能
        roleService.delRoleById(id);
        //响应
        return Result.success();
    }

    @PostMapping("/update")
    public Result<?> updateRole(@RequestBody addRole role) {
        //记录日志
        log.info("修改角色详情:{}", role);
        roleService.updateRole(role);
        return Result.success("");
    }
}
